﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PInvokeTest
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public class ID
    {
        public int id;
        string name;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public class NSJointLoad
    {
        //public int id;
        //[MarshalAs(UnmanagedType.I1)]
        //private bool global;
        public int id;
        public double fx;
        public double fy;
        private double fz;
        private double mx;
        private double my;
        private double mz;
        

        //public double Fx
        //{
        //    get { return this.fx; }
        //    set { this.fx = value; }
        //}
        //public double Fy
        //{
        //    get { return this.fy; }
        //    set { this.fy = value; }
        //}
        //public double Fz
        //{
        //    get { return this.fz; }
        //    set { this.fz = value; }
        //}
        //public double Mx
        //{
        //    get { return this.mx; }
        //    set { this.mx = value; }
        //}
        //public double My
        //{
        //    get { return this.my; }
        //    set { this.my = value; }
        //}
        //public double Mz
        //{
        //    get { return this.mz; }
        //    set { this.mz = value; }
        //}
        //public bool Global
        //{
        //    get { return this.global; }
        //    set { this.global = value; }
        //}
    }

    public class Joint
    {
        private IntPtr objPtr;
        public Joint()
        {
            this.objPtr = UnsafenativeMethod.NewJoint();
        }
        public string[] strArr
        {
            set {
                UnsafenativeMethod.SetJointStrArr(this.objPtr, value.Length, value);
            }
        }
           
        public NSJointLoad[] loadArr
        {
            set 
            {
                //UnsafenativeMethod.SetJointLoadArr(this.objPtr,value.Length,value);
                IntPtr[] loadPtrs = new IntPtr[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    IntPtr objectItem= Marshal.AllocHGlobal(Marshal.SizeOf(value[i].GetType()));
                    Marshal.StructureToPtr(value[i], objectItem, false);
                    loadPtrs[i] = objectItem;
                }
                UnsafenativeMethod.SetJointLoadArr(this.objPtr, value.Length, loadPtrs);

                for (int i = 0; i < value.Length; i++)
                {
                    Marshal.FreeHGlobal(loadPtrs[i]);
                }
            }
        }
        public NSJointLoad[] loadArr2
        {
            set
            {
                //UnsafenativeMethod.SetJointLoadArr(this.objPtr,value.Length,value);
                IntPtr[] loadPtrs = new IntPtr[value.Length];
                for (int i = 0; i < value.Length; i++)
                {   
                    IntPtr objectItem = Marshal.AllocHGlobal(Marshal.SizeOf(value[i].GetType()));
                    Marshal.StructureToPtr(value[i], objectItem, false);
                    loadPtrs[i] = objectItem;
                }
                UnsafenativeMethod.SetJointLoadArr(this.objPtr, value.Length, loadPtrs);

                for (int i = 0; i < value.Length; i++)
                {
                    Marshal.FreeHGlobal(loadPtrs[i]);
                }
            }
        }

        public NSJointLoad jointLoad
        {
            set 
            {
                UnsafenativeMethod.SetJointLoad(this.objPtr,value);
            }
            get 
            {
                IntPtr jointLoad = UnsafenativeMethod.GetJointLoad(this.objPtr);
                NSJointLoad load =(NSJointLoad)Marshal.PtrToStructure(jointLoad, typeof(NSJointLoad));
                return load;

            }
        }

        public RectangleSection section
        {
            set {
                UnsafenativeMethod.SetJointSection(this.objPtr, value);
            }
        }
    }
}

